(in-package :iorek)

(defstruct (cl-symbol-table (:include symbol-table)))

(defmethod intern-symbol (symbol (symbol-table cl-symbol-table))
  (call-next-method)
  (let ((downcased-symbol-name (string-downcase (symbol-name symbol))))
    (unless (equal downcased-symbol-name (symbol-name symbol))
      (alias symbol downcased-symbol-name (symbol-namespace symbol))))
  symbol)

(defun import-cl-package (package &key (name (string-downcase (package-name package)))
                          (namespace *namespace*))
  (let ((symbol (intern name namespace))) ;;TODO error if already present?
    (setf (symbol-table symbol) (make-cl-symbol-table))
    (do+ ((for x (symbols-in package :internal :external)))
      (let ((sym (intern (cl:symbol-name x) symbol)))
        (if (boundp x)
            (if (fboundp x)
                (record-definition sym x :kind :lisp-function-and-variable)
                (record-definition sym x :kind :lisp-variable))
            (if (fboundp x)
                (record-definition sym x :kind :lisp-function))))) ;;TODO export, etc.
    symbol))

(export (import-cl-package (find-package :cl) :name "cl"))

(defmethod cl:print-object ((s symbol) stream)
  (print-object s stream))